package com.blank.gpstest;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.List;

public class MyTreeListViewAdapter<T> extends TreeListViewAdapter<T> {
    private final Context mContext;
    private int currentItem = -1;

    public MyTreeListViewAdapter(ListView mTree, Context context,
                                 List<Node> datas, int defaultExpandLevel, boolean isHide)
            throws IllegalArgumentException, IllegalAccessException {
        super(mTree, context, datas, defaultExpandLevel, isHide);

        this.mContext = context;
    }

    public MyTreeListViewAdapter(ListView mTree, Context context,
                                 List<Node> datas, int defaultExpandLevel, boolean isHide, String code)
            throws IllegalArgumentException, IllegalAccessException {
        super(mTree, context, datas, defaultExpandLevel, isHide, code);

        this.mContext = context;
    }

    public void setCurrentItem(int currentItem) {
        this.currentItem = currentItem;
    }

    @SuppressWarnings("unchecked")
    @Override
    public View getConvertView(Node node, int position, View convertView,
                               ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_list, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.icon = convertView
                    .findViewById(R.id.id_treenode_icon);
            viewHolder.label = convertView
                    .findViewById(R.id.id_treenode_name);
//            viewHolder.checkBox = convertView.findViewById(R.id.id_treeNode_check);
//            viewHolder.id_treenode_icon1 = convertView.findViewById(R.id.id_treenode_icon1);

            convertView.setTag(viewHolder);

        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        if (node.getIcon() == -1) {
//            viewHolder.id_treenode_icon1.setVisibility(View.VISIBLE);
            viewHolder.icon.setVisibility(View.GONE);

        } else {
//            viewHolder.id_treenode_icon1.setVisibility(View.GONE);
            viewHolder.icon.setVisibility(View.VISIBLE);
            viewHolder.icon.setImageResource(node.getIcon());

        }

//        if (node.isHideChecked()) {
//            viewHolder.checkBox.setVisibility(View.GONE);
//        } else {
//            viewHolder.checkBox.setVisibility(View.VISIBLE);
//            setCheckBoxBg(viewHolder.checkBox, node.isChecked());
//        }
        viewHolder.label.setText(node.getName());
        //未选中
        viewHolder.label.setSelected(currentItem == position);
//        viewHolder.id_treenode_icon1.setSelected(currentItem == position);

        return convertView;
    }

    /**
     * checkbox是否显示
     *
     * @param cb
     * @param isChecked
     */
    private void setCheckBoxBg(CheckBox cb, boolean isChecked) {
//        if (isChecked) {
//            cb.setBackgroundResource(R.drawable.checkbox_checked_ic);
//        } else
//            cb.setBackgroundResource(R.drawable.checkbox_selector);
    }

    /**
     * 给TextView右边设置图片
     *
     * @param resId
     * @param
     */
    private void setTextImage(int resId, TextView v) {
        Drawable drawable = mContext.getResources().getDrawable(resId);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());// 必须设置图片大小，否则不显示
        v.setCompoundDrawables(drawable, null, null, null);
    }

    private final class ViewHolder {
        ImageView icon;
        //        ImageView id_treenode_icon1;
        TextView label;
//        CheckBox checkBox;
    }
}
